<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<input type="text" id="text1">
<input type="text" id="text2">
<input value="开始测试" type="button" onclick="method()">

</body>

<script>

    function method() {
        var elementById1 = document.getElementById("text1");
        var elementById2 = document.getElementById("text2");
        var str1 = elementById1.value;
        var str2 = elementById2.value;


        var indix = -1;
        var geShu = 0;
        while ((indix = str2.indexOf(str1)) != -1) {

            alert(indix + "==" + str2 + "==" + (str2.indexOf(str1) != -1))
            geShu++;
            str2 = str2.substring(indix + 1)
        }

        alert(geShu )
    }
</script>
</html>